import http from "@/utils/index";
import { signatureGenerate } from "@/utils/md5";
import { useUserStore } from "@/store/userStore";
const publicHttp = (data, url, method = "POST") => {
  const userStore = useUserStore();
  // 用空字符串替换 undefined 值
  const newData = Object.fromEntries(
    Object.entries(data).map(([key, value]) => [
      key,
      value === undefined ? "" : value,
    ])
  );
  // let str = signatureGenerate(newData);
  newData.token = userStore.user.token || "";
  // newData.sign = str.sign;
  // newData.timestamp = str.timestamp;
  // switch (userStore.user.system) {
  //   case 'recommendation':
  //     newData.province = userStore.user.province;
  //     break;
  // }

  // 根据 method 判断请求类型
  return http({
    url: url,
    method: method,
    [method === "GET" ? "params" : "data"]: newData, // 动态设置 params 或 data
  }).then((res) => {
    // 统一处理响应
    return res?.data.status === 200 ? res.data.data : {};
  });
};

export default publicHttp;
